<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" integrity="sha256-Z1K5uhUaJXA7Ll0XrZ/0JhX4lAtZFpT6jkKrEDT0drU=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.14.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":-1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="REST APIMany objects of Jenkins provide the remote access API. They are available at &#x2F;...&#x2F;api&#x2F; where “…” portion is the object for which you’d like to access.  XML API Access data exposed in HTML as X">
<meta property="og:type" content="article">
<meta property="og:title" content="Jenkins-API">
<meta property="og:url" content="http://example.com/2022/05/13/Jenkins-API/index.html">
<meta property="og:site_name" content="JsyBlog">
<meta property="og:description" content="REST APIMany objects of Jenkins provide the remote access API. They are available at &#x2F;...&#x2F;api&#x2F; where “…” portion is the object for which you’d like to access.  XML API Access data exposed in HTML as X">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-05-12T18:43:18.000Z">
<meta property="article:modified_time" content="2022-06-07T07:20:39.373Z">
<meta property="article:author" content="SongyangJi">
<meta property="article:tag" content="Jenkins">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/2022/05/13/Jenkins-API/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://example.com/2022/05/13/Jenkins-API/","path":"2022/05/13/Jenkins-API/","title":"Jenkins-API"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Jenkins-API | JsyBlog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">JsyBlog</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#REST-API"><span class="nav-number">1.</span> <span class="nav-text">REST API</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Controlling-the-amount-of-data-you-fetch"><span class="nav-number">1.1.</span> <span class="nav-text">Controlling the amount of data you fetch</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Create-Job"><span class="nav-number">1.2.</span> <span class="nav-text">Create Job</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Copy-Job"><span class="nav-number">1.3.</span> <span class="nav-text">Copy Job</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Build-Queue"><span class="nav-number">1.4.</span> <span class="nav-text">Build Queue</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Load-Statistics"><span class="nav-number">1.5.</span> <span class="nav-text">Load Statistics</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Restarting-Jenkins"><span class="nav-number">1.6.</span> <span class="nav-text">Restarting Jenkins</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8-Jenkins-API%E7%9A%84Java%E5%AE%A2%E6%88%B7%E7%AB%AF"><span class="nav-number">2.</span> <span class="nav-text">使用 Jenkins API的Java客户端</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Maven%E4%BE%9D%E8%B5%96"><span class="nav-number">2.1.</span> <span class="nav-text">Maven依赖</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%BF%9E%E6%8E%A5Jenkins"><span class="nav-number">2.2.</span> <span class="nav-text">连接Jenkins</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%93%8D%E4%BD%9CJob"><span class="nav-number">2.3.</span> <span class="nav-text">操作Job</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%9B%E5%BB%BAJob"><span class="nav-number">2.3.1.</span> <span class="nav-text">创建Job</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9F%A5%E7%9C%8Bxml%E5%AE%9A%E4%B9%89"><span class="nav-number">2.3.1.1.</span> <span class="nav-text">查看xml定义</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%BF%90%E8%A1%8C%E4%BB%A3%E7%A0%81"><span class="nav-number">2.3.1.2.</span> <span class="nav-text">运行代码</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#CLI"><span class="nav-number">3.</span> <span class="nav-text">CLI</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">SongyangJi</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">251</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">45</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">109</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2022/05/13/Jenkins-API/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="SongyangJi">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="JsyBlog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Jenkins-API | JsyBlog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Jenkins-API
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-05-13 02:43:18" itemprop="dateCreated datePublished" datetime="2022-05-13T02:43:18+08:00">2022-05-13</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2022-06-07 15:20:39" itemprop="dateModified" datetime="2022-06-07T15:20:39+08:00">2022-06-07</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Jenkins/" itemprop="url" rel="index"><span itemprop="name">Jenkins</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="REST-API"><a href="#REST-API" class="headerlink" title="REST API"></a>REST API</h1><p>Many objects of Jenkins provide the remote access API. They are available at <code>/.../api/</code> where “…” portion is the object for which you’d like to access.</p>
<ul>
<li><p><a target="_blank" rel="noopener" href="http://211.87.224.233:18080/api/xml">XML API</a></p>
<p>Access data exposed in <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/">HTML</a> as XML for machine consumption. <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/api/schema">Schema</a> is also available.You can also specify optional XPath to control the fragment you’d like to obtain (but see <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/api/#tree">below</a>). For example, <code>../api/xml?xpath=/*/*[0]</code>.For XPath that matches multiple nodes, you need to also specify the “wrapper” query parameter to specify the name of the root XML element to be create so that the resulting XML becomes well-formed.Similarly <code>exclude</code> query parameter can be used to exclude nodes that match the given XPath from the result. This is useful for trimming down the amount of data you fetch (but again see <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/api/#tree">below</a>). This query parameter can be specified multiple times.XPath filtering is powerful, and you can have it only return a very small data, but note that the server still has to build a full DOM of the raw data, which could cause a large memory spike. To avoid overloading the server, consider using the <code>tree</code> parameter, or use the <code>xpath</code> parameter in conjunction with the <code>tree</code> parameter. When used together, the result of the <code>tree</code> parameter filtering is built into DOM, then the XPath is applied to compute the final return value. In this way, you can often substantially reduce the size of DOM built in memory.</p>
</li>
<li><p><a target="_blank" rel="noopener" href="http://211.87.224.233:18080/api/json?pretty=true">JSON API</a></p>
<p>Access the same data as JSON for JavaScript-based access. <code>tree</code> may be used.</p>
</li>
<li><p><a target="_blank" rel="noopener" href="http://211.87.224.233:18080/api/python?pretty=true">Python API</a></p>
<p>Access the same data as Python for Python clients. This can be parsed into Python objects as <code>ast.literal_eval(urllib.urlopen(&quot;...&quot;).read())</code> and the resulting object tree is identical to that of JSON.</p>
</li>
</ul>
<p>For more information about remote API in Jenkins, see <a target="_blank" rel="noopener" href="https://www.jenkins.io/redirect/remote-api">the documentation</a>.</p>
<h2 id="Controlling-the-amount-of-data-you-fetch"><a href="#Controlling-the-amount-of-data-you-fetch" class="headerlink" title="Controlling the amount of data you fetch"></a>Controlling the amount of data you fetch</h2><p>The <code>tree</code> query parameter allows you to explicitly specify and retrieve only the information you are looking for, by using an XPath-ish path expression. The value should be a list of property names to include, with sub-properties inside square braces. Try [tree&#x3D;jobs[name],views[name,jobs[name]]](<a target="_blank" rel="noopener" href="http://211.87.224.233:18080/api/xml?tree=jobs%5Bname%5D,views%5Bname,jobs%5Bname%5D%5D">http://211.87.224.233:18080/api/xml?tree=jobs[name],views[name,jobs[name]]</a>) to see just a list of jobs (only giving the name) and views (giving the name and jobs they contain). <strong>Note</strong>: for array-type properties (such as <code>jobs</code> in this example), the name must be given in the original plural, not in the singular as the element would appear in XML (<code>&lt;job&gt;</code>). This will be more natural for e.g. [json?tree&#x3D;jobs<a target="_blank" rel="noopener" href="http://211.87.224.233:18080/api/json?tree=jobs%5Bname%5D">name]</a> anyway: the JSON writer does not do plural-to-singular mangling because arrays are represented explicitly.</p>
<p>For array-type properties, a range specifier is supported. For example, <code>tree=jobs[name]&#123;0,10&#125;</code> would retrieve the name of the first 10 jobs. The range specifier has the following variants:</p>
<ul>
<li><strong>{M,N}</strong>: From the M-th element (inclusive) to the N-th element (exclusive).</li>
<li><strong>{M,}</strong>: From the M-th element (inclusive) to the end.</li>
<li><strong>{,N}</strong>: From the first element (inclusive) to the N-th element (exclusive). The same as {0,N}.</li>
<li><strong>{N}</strong>: Just retrieve the N-th element. The same as {N,N+1}.</li>
</ul>
<p>Another way to retrieve more data is to use the <code>depth=N</code> query parameter. This retrieves all the data up to the specified depth. Compare <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/api/xml">depth&#x3D;0</a> and <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/api/xml?depth=1">depth&#x3D;1</a> and see what the difference is for yourself. Also note that data created by a smaller depth value is always a subset of the data created by a bigger depth value.</p>
<p>Because of the size of the data, the <code>depth</code> parameter should really be only used to explore what data Jenkins can return. Once you identify the data you want to retrieve, you can then come up with the <code>tree</code> parameter to exactly specify the data you need.</p>
<h2 id="Create-Job"><a href="#Create-Job" class="headerlink" title="Create Job"></a>Create Job</h2><p>要创建新的Job，请使用查询参数发布config.xml到此URL（http:&#x2F;&#x2F;{jenkinsUrl}&#x2F;createItem ），带上请求参数<code>name=*JOBNAME*</code>，请求头上带着<code>Content-Type: application/xml</code> 。</p>
<p>如果创建成功，您将获得 200 状态码，如果创建失败，您将获得 4xx&#x2F;5xx 码。</p>
<p><code>config.xml</code>是 Jenkins 用于将Job（无论是FreeStyle、Pipeline，还是其他类型的）存储在文件系统中的文件格式，因此可以在 Jenkins 主目录中查看它们的示例，或者通过直接从<code>/job/*JOBNAME*/config.xml</code>查询已经存在的job的xml定义。</p>
<h2 id="Copy-Job"><a href="#Copy-Job" class="headerlink" title="Copy Job"></a>Copy Job</h2><p>为了复制一个已有的Job，发送Post请求到此URL（http:&#x2F;&#x2F;{jenkinsUrl}&#x2F;createItem ），带上请求参数<code>name=*NEWJOBNAME*&amp;mode=copy&amp;from=*FROMJOBNAME*</code>。</p>
<h2 id="Build-Queue"><a href="#Build-Queue" class="headerlink" title="Build Queue"></a>Build Queue</h2><p>Build queue has <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/queue/api/">its own separate API</a>.</p>
<h2 id="Load-Statistics"><a href="#Load-Statistics" class="headerlink" title="Load Statistics"></a>Load Statistics</h2><p>Overall load statistics of Jenkins has <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/overallLoad/api/">its own separate API</a>.</p>
<h2 id="Restarting-Jenkins"><a href="#Restarting-Jenkins" class="headerlink" title="Restarting Jenkins"></a>Restarting Jenkins</h2><p>Jenkins will enter into the “quiet down” mode by sending a POST request with optional <code>reason</code> query parameter to <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/quietDown">this URL</a>. You can also send another request to this URL to update the reason. You can cancel this mode by sending a POST request to <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/cancelQuietDown">this URL</a>. On environments where Jenkins can restart itself (such as when Jenkins is installed as a Windows service), POSTing to <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/restart">this URL</a> will start the restart sequence, or <a target="_blank" rel="noopener" href="http://211.87.224.233:18080/safeRestart">this URL</a> to restart once no jobs are running. All these URLs need Overall&#x2F;Manage permission (typically granted via Overall&#x2F;Administer).</p>
<blockquote>
<p>参考文档</p>
<p><a target="_blank" rel="noopener" href="https://ci.jenkins.io/api/">https://ci.jenkins.io/api/</a></p>
<p><a target="_blank" rel="noopener" href="https://www.jenkins.io/doc/book/using/remote-access-api/">https://www.jenkins.io/doc/book/using/remote-access-api/</a></p>
</blockquote>
<h1 id="使用-Jenkins-API的Java客户端"><a href="#使用-Jenkins-API的Java客户端" class="headerlink" title="使用 Jenkins API的Java客户端"></a>使用 Jenkins API的Java客户端</h1><h2 id="Maven依赖"><a href="#Maven依赖" class="headerlink" title="Maven依赖"></a>Maven依赖</h2><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--jenkins-java-client--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.offbytwo.jenkins<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jenkins-client<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.3.8<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h2 id="连接Jenkins"><a href="#连接Jenkins" class="headerlink" title="连接Jenkins"></a>连接Jenkins</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.sdu.jsy.learnjenkins.config;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: SongyangJi</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>:</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@since</span>: 2022/5/17</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="meta">@ConfigurationProperties(value = &quot;jenkins&quot;)</span></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="meta">@Getter</span></span><br><span class="line"><span class="meta">@Setter</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JenkinsConfig</span> &#123;</span><br><span class="line"></span><br><span class="line">    String url;</span><br><span class="line">    String username;</span><br><span class="line">    String token;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     * JenkinsHttpClient 和 JenkinsServer 关系：</span></span><br><span class="line"><span class="comment">     * JenkinsServer 是对 JenkinsHttpClient某些功能的更高层次的封装，</span></span><br><span class="line"><span class="comment">     * JenkinsServer依赖于JenkinsHttpClient</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Http 客户端工具</span></span><br><span class="line"><span class="comment">     * 如果有些 API 该工具包未提供，可以用此Http客户端操作远程接口，执行命令</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="keyword">public</span> JenkinsHttpClient <span class="title function_">getHttpClient</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="type">JenkinsHttpClient</span> <span class="variable">jenkinsHttpClient</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            jenkinsHttpClient = <span class="keyword">new</span> <span class="title class_">JenkinsHttpClient</span>(<span class="keyword">new</span> <span class="title class_">URI</span>(url), username, token);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (URISyntaxException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> jenkinsHttpClient;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 连接 Jenkins</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="keyword">public</span> JenkinsServer <span class="title function_">connectServer</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="type">JenkinsServer</span> <span class="variable">jenkinsServer</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            jenkinsServer = <span class="keyword">new</span> <span class="title class_">JenkinsServer</span>(<span class="keyword">new</span> <span class="title class_">URI</span>(url), username, token);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (URISyntaxException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> jenkinsServer;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>






<h2 id="操作Job"><a href="#操作Job" class="headerlink" title="操作Job"></a>操作Job</h2><p>已有一条工作流：<a target="_blank" rel="noopener" href="http://localhost:8080/job/pipeline-test/">http://localhost:8080/job/pipeline-test/</a></p>
<h3 id="创建Job"><a href="#创建Job" class="headerlink" title="创建Job"></a>创建Job</h3><h4 id="查看xml定义"><a href="#查看xml定义" class="headerlink" title="查看xml定义"></a>查看xml定义</h4><p>查看定义它的xml：<a target="_blank" rel="noopener" href="http://localhost:8080/job/pipeline-test/config.xml">http://localhost:8080/job/pipeline-test/config.xml</a></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&#x27;1.1&#x27; encoding=&#x27;UTF-8&#x27;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">flow-definition</span> <span class="attr">plugin</span>=<span class="string">&quot;workflow-job@1180.v04c4e75dce43&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">actions</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobAction</span> <span class="attr">plugin</span>=<span class="string">&quot;pipeline-model-definition@2.2077.vc78ec45162f1&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobPropertyTrackerAction</span> <span class="attr">plugin</span>=<span class="string">&quot;pipeline-model-definition@2.2077.vc78ec45162f1&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">jobProperties</span>/&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">triggers</span>/&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">parameters</span>/&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">options</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobPropertyTrackerAction</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">actions</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">description</span>&gt;</span><span class="tag">&lt;/<span class="name">description</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">keepDependencies</span>&gt;</span>false<span class="tag">&lt;/<span class="name">keepDependencies</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">properties</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty</span> <span class="attr">plugin</span>=<span class="string">&quot;gitlab-plugin@1.5.31&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">gitLabConnection</span>&gt;</span><span class="tag">&lt;/<span class="name">gitLabConnection</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">jobCredentialId</span>&gt;</span><span class="tag">&lt;/<span class="name">jobCredentialId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">useAlternativeCredential</span>&gt;</span>false<span class="tag">&lt;/<span class="name">useAlternativeCredential</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">hudson.model.ParametersDefinitionProperty</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">parameterDefinitions</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">net.uaznia.lukanus.hudson.plugins.gitparameter.GitParameterDefinition</span> <span class="attr">plugin</span>=<span class="string">&quot;git-parameter@0.9.16&quot;</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">name</span>&gt;</span>branch_tag<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">uuid</span>&gt;</span>912c32f3-e614-420d-80f3-f8086c4691d6<span class="tag">&lt;/<span class="name">uuid</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">type</span>&gt;</span>PT_BRANCH_TAG<span class="tag">&lt;/<span class="name">type</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">branch</span>&gt;</span><span class="tag">&lt;/<span class="name">branch</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">tagFilter</span>&gt;</span>*<span class="tag">&lt;/<span class="name">tagFilter</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">branchFilter</span>&gt;</span>.*<span class="tag">&lt;/<span class="name">branchFilter</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">sortMode</span>&gt;</span>NONE<span class="tag">&lt;/<span class="name">sortMode</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">defaultValue</span>&gt;</span>origin/master<span class="tag">&lt;/<span class="name">defaultValue</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">selectedValue</span>&gt;</span>NONE<span class="tag">&lt;/<span class="name">selectedValue</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">quickFilterEnabled</span>&gt;</span>false<span class="tag">&lt;/<span class="name">quickFilterEnabled</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">listSize</span>&gt;</span>5<span class="tag">&lt;/<span class="name">listSize</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">requiredParameter</span>&gt;</span>false<span class="tag">&lt;/<span class="name">requiredParameter</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">net.uaznia.lukanus.hudson.plugins.gitparameter.GitParameterDefinition</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">hudson.model.StringParameterDefinition</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">name</span>&gt;</span>port<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">defaultValue</span>&gt;</span>8080<span class="tag">&lt;/<span class="name">defaultValue</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">trim</span>&gt;</span>false<span class="tag">&lt;/<span class="name">trim</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">hudson.model.StringParameterDefinition</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">parameterDefinitions</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">hudson.model.ParametersDefinitionProperty</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">triggers</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">com.dabsquared.gitlabjenkins.GitLabPushTrigger</span> <span class="attr">plugin</span>=<span class="string">&quot;gitlab-plugin@1.5.31&quot;</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">spec</span>&gt;</span><span class="tag">&lt;/<span class="name">spec</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">triggerOnPush</span>&gt;</span>true<span class="tag">&lt;/<span class="name">triggerOnPush</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">triggerToBranchDeleteRequest</span>&gt;</span>false<span class="tag">&lt;/<span class="name">triggerToBranchDeleteRequest</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">triggerOnMergeRequest</span>&gt;</span>false<span class="tag">&lt;/<span class="name">triggerOnMergeRequest</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">triggerOnlyIfNewCommitsPushed</span>&gt;</span>false<span class="tag">&lt;/<span class="name">triggerOnlyIfNewCommitsPushed</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">triggerOnPipelineEvent</span>&gt;</span>false<span class="tag">&lt;/<span class="name">triggerOnPipelineEvent</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">triggerOnAcceptedMergeRequest</span>&gt;</span>true<span class="tag">&lt;/<span class="name">triggerOnAcceptedMergeRequest</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">triggerOnClosedMergeRequest</span>&gt;</span>false<span class="tag">&lt;/<span class="name">triggerOnClosedMergeRequest</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">triggerOnApprovedMergeRequest</span>&gt;</span>true<span class="tag">&lt;/<span class="name">triggerOnApprovedMergeRequest</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">triggerOpenMergeRequestOnPush</span>&gt;</span>never<span class="tag">&lt;/<span class="name">triggerOpenMergeRequestOnPush</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">triggerOnNoteRequest</span>&gt;</span>true<span class="tag">&lt;/<span class="name">triggerOnNoteRequest</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">noteRegex</span>&gt;</span>Jenkins please retry a build<span class="tag">&lt;/<span class="name">noteRegex</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">ciSkip</span>&gt;</span>true<span class="tag">&lt;/<span class="name">ciSkip</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">skipWorkInProgressMergeRequest</span>&gt;</span>true<span class="tag">&lt;/<span class="name">skipWorkInProgressMergeRequest</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">labelsThatForcesBuildIfAdded</span>&gt;</span><span class="tag">&lt;/<span class="name">labelsThatForcesBuildIfAdded</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">setBuildDescription</span>&gt;</span>true<span class="tag">&lt;/<span class="name">setBuildDescription</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">branchFilterType</span>&gt;</span>All<span class="tag">&lt;/<span class="name">branchFilterType</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">includeBranchesSpec</span>&gt;</span><span class="tag">&lt;/<span class="name">includeBranchesSpec</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">excludeBranchesSpec</span>&gt;</span><span class="tag">&lt;/<span class="name">excludeBranchesSpec</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">sourceBranchRegex</span>&gt;</span><span class="tag">&lt;/<span class="name">sourceBranchRegex</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">targetBranchRegex</span>&gt;</span><span class="tag">&lt;/<span class="name">targetBranchRegex</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">secretToken</span>&gt;</span>&#123;AQAAABAAAAAQGEXb6DKS0zJvyDXcbayiGrgglRQ7E35la+RU6p8KpAc=&#125;<span class="tag">&lt;/<span class="name">secretToken</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">pendingBuildName</span>&gt;</span><span class="tag">&lt;/<span class="name">pendingBuildName</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">cancelPendingBuildsOnUpdate</span>&gt;</span>false<span class="tag">&lt;/<span class="name">cancelPendingBuildsOnUpdate</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">com.dabsquared.gitlabjenkins.GitLabPushTrigger</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">triggers</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">properties</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">definition</span> <span class="attr">class</span>=<span class="string">&quot;org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition&quot;</span> <span class="attr">plugin</span>=<span class="string">&quot;workflow-cps@2689.v434009a_31b_f1&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-handlebars"><span class="language-xml">pipeline &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">    agent any</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">    </span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">    stages &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        stage(<span class="symbol">&amp;apos;</span>Stage 1: Fetch code from git<span class="symbol">&amp;apos;</span>) &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            steps &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                echo <span class="symbol">&amp;apos;</span>Stage 1: Fetch code from git -- SUCCESS<span class="symbol">&amp;apos;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            &#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        &#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars"></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        stage(<span class="symbol">&amp;apos;</span>Stage 2: Build the project using maven<span class="symbol">&amp;apos;</span>) &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            steps &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                echo <span class="symbol">&amp;apos;</span>Stage 2: Build the project using maven -- SUCCESS<span class="symbol">&amp;apos;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            &#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        &#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars"></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        stage(<span class="symbol">&amp;apos;</span>Stage 3: Make a custom image using docker<span class="symbol">&amp;apos;</span>) &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            steps &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                echo <span class="symbol">&amp;apos;</span>Stage 3: Make a custom image using docker -- SUCCESS<span class="symbol">&amp;apos;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            &#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        &#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars"></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        stage(<span class="symbol">&amp;apos;</span>Stage 4: Push image to Harbor<span class="symbol">&amp;apos;</span>) &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            steps &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                echo <span class="symbol">&amp;apos;</span>Stage 4: Push image to Harbor -- SUCCESS<span class="symbol">&amp;apos;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            &#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        &#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars"></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        stage(<span class="symbol">&amp;apos;</span>Stage 5: Publish over SSH<span class="symbol">&amp;apos;</span>) &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            steps &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                echo <span class="symbol">&amp;apos;</span>Stage 5: Publish over SSH -- SUCCESS<span class="symbol">&amp;apos;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            &#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        &#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars"></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">    &#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">&#125;</span></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">sandbox</span>&gt;</span>true<span class="tag">&lt;/<span class="name">sandbox</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">definition</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">triggers</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">disabled</span>&gt;</span>false<span class="tag">&lt;/<span class="name">disabled</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">flow-definition</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>其实此xml存放于<code>$JENKINS_HOME/jobs/pipeline-test/config.xml</code>，也就是<code>$JENKINS_HOME/jobs/$JOB/config.xml</code>这个文件。</p>
<h4 id="运行代码"><a href="#运行代码" class="headerlink" title="运行代码"></a>运行代码</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">jenkinsServer.createJob(<span class="string">&quot;pipeline2&quot;</span>, xml); <span class="comment">// xml如上</span></span><br></pre></td></tr></table></figure>
<p>抛出异常<code>org.apache.http.client.HttpResponseException: status code: 403, reason phrase: Forbidden</code> 。</p>
<blockquote>
<p>解决方案：<a target="_blank" rel="noopener" href="https://blog.csdn.net/ccc_bigdata/article/details/108080084">https://blog.csdn.net/ccc_bigdata/article/details/108080084</a></p>
</blockquote>
<h1 id="CLI"><a href="#CLI" class="headerlink" title="CLI"></a>CLI</h1><p>下载客户端jar：<a target="_blank" rel="noopener" href="http://211.87.224.233:18080/cli/">http://211.87.224.233:18080/cli/</a></p>
<ul>
<li><p>用法</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -jar jenkins-cli.jar [-s URL] command [opts...] args...</span><br></pre></td></tr></table></figure>
</li>
<li><p>示例</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -jar jenkins-cli.jar -s http://211.87.224.233:18080/ -auth admin:admin -webSocket help</span><br></pre></td></tr></table></figure></li>
</ul>
<blockquote>
<p> 参考文档</p>
<p><a target="_blank" rel="noopener" href="http://www.mydlq.club/article/23/">http://www.mydlq.club/article/23/</a></p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Jenkins/" rel="tag"># Jenkins</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/05/09/IP%E5%9C%B0%E5%9D%800.0.0.0%E6%98%AF%E4%BB%80%E4%B9%88%E6%84%8F%E6%80%9D%EF%BC%9F/" rel="prev" title="IP地址0.0.0.0是什么意思？">
                  <i class="fa fa-chevron-left"></i> IP地址0.0.0.0是什么意思？
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/05/14/Homebrew%E6%9B%B4%E6%8D%A2%E9%95%9C%E5%83%8F%E6%BA%90/" rel="next" title="Homebrew更换镜像源">
                  Homebrew更换镜像源 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">SongyangJi</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>





  





</body>
</html>
